package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.dto.CaseOperatorForm;
import cn.livingCloud.dto.JsonDto;
import cn.livingCloud.entity.core.Case;
import cn.livingCloud.entity.core.CaseRecord;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.entity.user.User;
import cn.livingCloud.repository.CaseMapper;
import cn.livingCloud.repository.CaseRecordMapper;
import cn.livingCloud.service.impl.WxTemplateMsgServiceImpl;
import cn.livingCloud.utils.SessionUtils;

import java.util.List;

/**
 * Created by 唐旭 on 2016/6/6.
 */
public abstract class CaseOperator {
    protected transient CaseMapper caseMapper = BeanProvider.getBean(CaseMapper.class);
    protected transient CaseRecordMapper caseRecordMapper = BeanProvider.getBean(CaseRecordMapper.class);
    protected transient WxTemplateMsgServiceImpl msgService = BeanProvider.getBean(WxTemplateMsgServiceImpl.class);

    protected CaseOperatorForm form;
    protected Case aCase;
    protected CaseRecord record;

    public CaseOperator() {
    }

    public CaseOperator(CaseOperatorForm form) {
        this.form = form;
    }

    public CaseOperatorForm getForm() {
        return form;
    }

    public CaseOperator setForm(CaseOperatorForm form) {
        this.form = form;
        return this;
    }

    public JsonDto update() {
//        String check = check();
//        if (StringUtils.isNotEmpty(check)) {
//            return createErrorJsonDto(check);
//        }
        loadCase();
        createRecord();

        JsonDto result = operation();
        List<Employee> newOwners = getNewOwners();
        if (newOwners != null && newOwners.size() > 0) {
            aCase.setOwnerId(buildEmployeeUuidArray(newOwners).toString());
        }
        if (result.isState()) {
            save();
        }
        return result;
    }

//    /**
//     * 检查数据
//     *
//     * @return 错误信息, 如果为空则表示通过检查
//     */
//    protected abstract String check();

    /**
     * 创建一个jsonDto
     *
     * @param msg 错误消息
     * @return jsonDto实例
     */
    private JsonDto createErrorJsonDto(String msg) {
        JsonDto jsonDto = new JsonDto();
        jsonDto.setState(false);
        jsonDto.setMsg(msg);
        return jsonDto;
    }

    protected abstract JsonDto operation();

    /**
     * 加载 案件
     */
    protected void loadCase() {
        aCase = caseMapper.selectByPrimaryKey(form.getCaseUuid());
    }

    /**
     * 保存更改
     */
    protected void save() {
        caseMapper.updateByPrimaryKey(aCase);
        caseRecordMapper.insert(record);
    }

    /**
     * 创建 案件档案
     */
    protected void createRecord() {
        record = new CaseRecord();
        record.setContent(getContent())
                .setCaseUuid(form.getCaseUuid())
                .setType(form.getRecordType())
                .setOldOwnerId(aCase.getOwnerId());
        List<Employee> owners = getNewOwners();
        if (owners != null && owners.size() > 0) {
            record.setNewOwnerId(buildEmployeeUuidArray(owners).toString());
        }
        Employee employee = SessionUtils.getAttribute(Employee.class, SessionConstants.EMPLOYEE_KEY);
        if (employee != null) {
            record.setOperator(employee.getUuid());
        }
        User currentUser = (User) SessionUtils.getCurrentUser();
        if (currentUser != null) {
            record.setOperatorUserUuid(currentUser.getUuid());
        }
    }

    protected StringBuilder buildEmployeeUuidArray(List<Employee> owners) {
        StringBuilder ownersString = new StringBuilder();
        for (Employee owner : owners) {
            ownersString.append(owner.getUuid()).append(",");
        }
        ownersString = ownersString.deleteCharAt(ownersString.length() - 1);
        return ownersString;
    }

    protected String getContent() {
        return form.getContent();
    }


    /**
     * 获取新的所有者ID
     *
     * @return 案件归属者
     */
    protected abstract List<Employee> getNewOwners();
}
